// pages/my_qiye/my_qiye.js
const app = getApp();
const $get_api = require('../../common/api').HTTP;
const wx_auth = require('../../common/wx_auth') //微信授权js
const util = require('../../utils/util')
Page({

  data: {
    wx_auth:wx_auth.authApply,
    util:util,
    userInfo:{},
    activeIndex: 0,
    scrollHeight:null,
    isLoginBtn:true,
    daka_img_bg:app.imgUrl + "daka_img_bg.png"+ app.version,
    right_row:app.imgUrl + "right_row.png"+ app.version,
    activeList:[],
    index_bg:app.imgUrl + "index_bg.jpg"+ app.version,
    swiperCurrent: 0, //当前导航点
    page: 0, // page：当前页--
    icon_list: [
        { id:1,img:app.imgUrl + "my_qiye/img1.png"+ app.version,text:"酒店"}, 
        { id:2,img:app.imgUrl + "my_qiye/img2.png"+ app.version,text:"餐饮"},
        { id:3,img:app.imgUrl + "my_qiye/img2.png"+ app.version,text:"文旅"},
        { id:4,img:app.imgUrl + "my_qiye/img2.png"+ app.version,text:"工农业"},
        { id:5,img:app.imgUrl + "my_qiye/img2.png"+ app.version,text:"大健康"},
        { id:6,img:app.imgUrl + "my_qiye/img2.png"+ app.version,text:"地产"},
        { id:7,img:app.imgUrl + "my_qiye/img2.png"+ app.version,text:"金融"},
        { id:8,img:app.imgUrl + "my_qiye/img2.png"+ app.version,text:"科技"},
        { id:9,img:app.imgUrl + "my_qiye/img2.png"+ app.version,text:"商贸"},
        { id:10,img:app.imgUrl + "my_qiye/img2.png"+ app.version,text:"食品"},
        

    ],
    keyword:'',// 搜索框内容，使用的是简易的双向绑定 //温习一下：第一层才能进行双向绑定
    keywords:[],//历史搜索的数组
    label_img:app.imgUrl + "my_qiye/label_img.png"+ app.version
  },
  onReady(){
    
  },
  onLoad() {

  },
  onShow(options){
    console.log(wx.getStorageSync('token'),"token");
    let _this = this;
    _this.data.login_status = wx_auth.checkToken()
    if(!_this.data.login_status){
        wx_auth.authApply(options,'');
    }
    if(wx_auth.checkToken(1)){
        _this.data.isLoginBtn = false;
        _this.bannerList(1)
        _this.navShow();
        _this.setListHeight();
        _this.post_search()
    }
    _this.setData({
        isLoginBtn: _this.data.isLoginBtn,
        keywords:[],
        keyword:""
    })
    
    
  },
   /**
     * 用户点击右上角分享
   */
  onShareAppMessage() {
    let _this = this;
    // 构建页面参数
    let params = app.getShareUrlParams({});  //app.js分享为封装
    return {
        title: app.useConst.SET_INFO.share_obj.title,
        //   path: "/pages/order_list/order_list?" + params,
        path: "/pages/index/index?" + params,
        imageUrl: app.useConst.SET_INFO.share_obj.imageUrl
    };
  },
  /**
     * 授权
     */
  getUserProfile: function (options) {
        let that = this;
        // wx_auth.authApply(options,this.data.page,app.useConst.LOGO_CONFIG.WX_LOGIN_TYPE);
        wx_auth.authApply(options,'');
  },
  navShow:function(){
      //控制导航栏出现
    if (typeof this.getTabBar === 'function' && this.getTabBar()) {
        this.getTabBar().setData({
            selected: 2
        })
    }
  },
  setListHeight() {
    let _this = this;
    wx.getSystemInfo({
      success: res => {
        _this.setData({
          scrollHeight: res.windowHeight - 48,
        });
      }
    });
  },
  receiveMessage:function(e){
    //接收H5传过来的数据
    let data = e.detail;
    console.log(data,"h5h5h5");
  },
  
  //图片变化时
  swiperChange: function (e) {
      this.setData({
          swiperCurrent: e.detail.current
      })
  },


    gotopage:function(e){
        let that = this;
        let u_page = e.currentTarget.dataset.upage;
        console.log(u_page,"u_page");
        if(u_page == 1){
            // wx.navigateTo({
            //   url: '../my_zhanbu/my_zhanbu',
            // })
        }
        else if(u_page == 2){
            // wx.navigateTo({
            //   url: '../my_question/my_question',
            // })
        }
    },
    bannerList:function(type = 1){
        let that = this;
        $get_api.getBanner({ 
            type:1
        }).then(r => { 
        let data = r.data.data;
        // console.log(data,"获取banner成功");
        that.data.activeList = [];
        that.data.activeList = data;
        that.setData({
            activeList:that.data.activeList
        })
      })
    },
    goto_search_page(e){
      //传类型 keywords传 ""
      let u_type = e.currentTarget.dataset.upage;
      let u_title = e.currentTarget.dataset.title;

      // console.log(u_type,"u_type");
      let new_keyword = ""
      if(u_type != 0){
        wx.navigateTo({
          url: '../company_search/company_search?cate_id='+ u_type +'&keyword=' + new_keyword + '&data_title='+u_title,
        })
      }else{
        return false
      }
    },
     //点击搜索按钮
     onSearch(){
      // this.data.keyword获取数据，
      //trim去除字符串前后多余空格
      const keyword=this.data.keyword.trim()
      //非空判断
      // if(keyword==='')
      // {
      //     return wx.showToast({
      //       icon:'none',
      //       title: '请输入搜索内容',
      //     })
      // }
      //在数组增加内容
      //push是增加在后面，unshift是在前面，历史记录当然是新的在前面
      //这个不会自动更新视图而微信小程序没用提供手动更新视图的api所以不用这个方法
      /* 
      this.data.keywords.unshift(keyword)
      */
     
     //增加数组长度的步骤也可以使用展开运算符
     const arr=[keyword,...this.data.keywords]

      this.setData({
          //先转化为set去重
          //然后重新转化为数组
          keywords:Array.from(new Set(arr)),
          //搜索完成清空搜索框
          keyword:''
      })
      wx.navigateTo({
        url: '../company_search/company_search?cate_id='+0+'&keyword='+keyword+'&data_title=全部',
      })
      console.log(this.data.keywords)
  },
  //清空历史记录
  onClear(){
    //清空历史记录数组
    this.setData({
        keywords:[]
    })
  },
  post_search(cate_id,keyword){
    let that = this;
      $get_api.getEnterpriseCate({ 
        a:1
    }).then(r => { 
        let data = r.data.data;
        console.log(data,"data");
        that.data.icon_list = []
        that.data.icon_list = data.list
        that.setData({
          icon_list:that.data.icon_list
        })
      }) 

  }
})
